package com.aichat.ai_chatbackend.controller;

import com.aichat.ai_chatbackend.common.BaseResponse;
import com.aichat.ai_chatbackend.model.dto.message.MessageQueryRequest;
import com.aichat.ai_chatbackend.model.entity.AiChatMemory;
import com.aichat.ai_chatbackend.model.enums.ErrorCode;
import com.aichat.ai_chatbackend.service.AiChatMemoryService;
import com.aichat.ai_chatbackend.utils.ResultUtils;
import com.aichat.ai_chatbackend.utils.exception.ThrowUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/message")
@Slf4j
public class ChatMessageController {

    private final AiChatMemoryService aiChatMemoryService;

    public ChatMessageController(AiChatMemoryService aiChatMemoryService){

        this.aiChatMemoryService = aiChatMemoryService;
    }

    @PostMapping("/get/history")
    public BaseResponse<Page<AiChatMemory>> getHistoryMessagesPage(@RequestBody MessageQueryRequest messageQueryRequest){
        ThrowUtils.throwIf(messageQueryRequest.getConversationId()==null, ErrorCode.PARAMS_ERROR);
        int current = messageQueryRequest.getCurrent();
        int pageSize = messageQueryRequest.getPageSize();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("conversation_id",messageQueryRequest.getConversationId());

        Page<AiChatMemory> chatMessagePage = aiChatMemoryService.page(
                new Page(current,pageSize),queryWrapper);
        return ResultUtils.success(chatMessagePage);

    }

}
